import { useRoute } from "vue-router";
import type { Router } from "vue-router";
import { Store } from "vuex";
import type { RootState } from "@/store/types";
import type { TabStatus, TabCache } from "@/store/modules/tab";
import { computed, reactive,onMounted } from "vue";
import type { Employee } from "@/store/modules/auth";
import request from "@/public/request";

/**
 * 当前用户信息
 */
 function employeeMenuRef(store: Store<RootState>, router: Router) {
    // 获得企业账号信息
    onMounted(() => {
      getEmployeeInfo();
    });
    // 所属企业资料信息
    const employee = computed<Employee | null>(() => {
      return store.state.auth.employee;
    });
  
    /**
     * 获得企业账号信息
     */
    async function getEmployeeInfo() {
      let res = await request.get("/auth/info");
      if (!res.user) {
        return;
      }
      store.commit("auth/setEmployee", res.user);
    }
  
    /**
     * 登出
     */
    async function logout() {
      await store.dispatch("auth/logout");
      window.location.hash = "#/"
      window.location.reload()
    }
  
    /**
     * 下拉菜单
     */
    function dropDownCommand(command: string) {
      switch (command) {
        case "chooseCompany":
          router.push("/chooseCompany");
          break;
        case "userinfo":
          router.push("/internal/userSetting");
          break;
        case "outLogin":
          logout();
          break;
      }
    }
  
    return reactive({
      employee,
      dropDownCommand,
      logout
    });
  }

  export default employeeMenuRef